package com.ekuaitu.kuaitu.activity;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ekuaitu.kuaitu.R;
import com.ekuaitu.kuaitu.base.BaseActivity;
import com.ekuaitu.kuaitu.widget.MyTimePicker;
import com.ekuaitu.kuaitu.widget.NumberButton;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

public class LongRentConditionActivity extends BaseActivity {

    ImageButton longRentAdd;
    @BindView(R.id.long_rent_return_time)
    TextView longRentReturnTime;
    @BindView(R.id.long_rent_rent_time)
    TextView longRentRentTime;
    @BindView(R.id.long_rent_radioButton)
    RadioButton longRentRadioButton;
    @BindView(R.id.progress_bar_rent_condition)
    RelativeLayout progressBarRentCondition;

    @BindView(R.id.number_button)
    NumberButton numberButton;
    public static final int INTENT_SEARCH_PARK = 1;
    private Context context = this;

    @Override
    public int getLayoutId() {
        return R.layout.activity_long_rentj_condition;
    }

    @Override
    public void initView() {
        numberButton.setMaxValue(30)
                .setAvaiValue(30)
                .setCurrentNumber(1)
                .setOnCountChangeListener(new NumberButton.OnCountChangeListener() {
                    @Override
                    public void onCountChange(int count) {

                    }
                });

    }


    @OnClick({R.id.rent_condition_return, R.id.long_rent_choose_time, R.id.long_rent_rent_park, R.id.long_rent_return_park, R.id.rent_condition_ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rent_condition_return:
                break;
            case R.id.long_rent_choose_time:
                MyTimePicker timePicker = new MyTimePicker(context, 7);
                timePicker.setOnTimeChosenListener(new MyTimePicker.OnTimeChosenListener() {
                    @Override
                    public void onTimeChosen(long timeMills) {
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
                        longRentRentTime.setText(simpleDateFormat.format(new Date(timeMills)));
                    }
                });
                timePicker.showPicker();
                break;
            case R.id.long_rent_rent_park:
                Intent intent = new Intent(this, SearchPositionActivity.class);
                startActivityForResult(intent, INTENT_SEARCH_PARK);
                break;
            case R.id.long_rent_return_park:
                Log.i("countresult", numberButton.getCount() + "");

                break;
            case R.id.rent_condition_ok:
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    v.clearFocus();
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    public boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                //  点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    @Override
    public void onBackPressed() {
        View v = getCurrentFocus();
        if (v != null && (v instanceof EditText)) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                v.clearFocus();
            }
        } else {
            super.onBackPressed();
        }

    }
}
